Param($NfoPath, $StartPath)

$all_start = Get-Date

$global:n_w_nfo = 0
$global:n_wo_nfo = 0
$global:n_nfo_copied = 0

Write-Output "Looking for movies which need nfo's in '$StartPath'..."

function CopyNfos([string] $WorkPath) {
#    Write-Output "Looking for movies which need nfo's in '$WorkPath'..."
    $items = Get-ChildItem $WorkPath | where {! $_.PSIsContainer}
    ForEach ($item in $items) {
        if ($item -match "(\d{2}[a-z]?_|s\d{2}e\d{2}_|)(.*?)(_SUBMUX|_se|_fixed|_CD.|_\d|)\.(avi|mkv|mp4)$") {
            $movie_pre = $matches[1]
            $movie_base = $matches[2]
            $movie_extra = $matches[3]
            $movie_ext = $matches[4]
            $nfo_src_name = $movie_base + ".nfo"
            $nfo_dst_name = $movie_pre + $movie_base + $movie_extra + ".nfo"
            $nfo_dst_fullname = Join-Path $WorkPath $nfo_dst_name
            if (!(Test-Path $nfo_dst_fullname)) {
                $global:n_wo_nfo++
#                Write-Host "$nfo_src_name" -f yellow -nonewline; Write-Host " --> " -nonewline
#                Write-Host "$nfo_dst_name" -f yellow
                $nfo_src_fullname = Join-Path $NfoPath $nfo_src_name
                if (Test-Path $nfo_src_fullname) {
                    Copy-Item $nfo_src_fullname $nfo_dst_fullname
                    $global:n_nfo_copied++
                    Write-Host "    Movie '" -nonewline; Write-Host "$item" -f green -nonewline
                    Write-Host "' had no nfo, but now it has!"
                }
                else {
                    if ($movie_base -match ".*[-\.](AnayRip|CHD|CtrlHD|DIAF|F7|fiend|HiDt|MiNiSUPRA|mSD|NO1KNOWS|PAPPiS|andreaspetersson|PublicHD|UNRELEASED)$") {
                        $group = $matches[1]
                        Write-Host "    Movie '$item' is ignored because of group '" -nonewline
                        Write-Host "$group" -f darkyellow -nonewline; Write-Host "'."
                    }
                    elseif ($movie_base -match ".*?\.(.DTV|TVRip|DSRip)\.") {
#                        Write-Host "    Movie '$item' is ignored because it is a " -nonewline
#                        Write-Host "TV" -f darkyellow -nonewline; Write-Host " release."
                    }
                    elseif ($movie_base -match ".*?\.WEBRiP\.") {
                        Write-Host "    Movie '$item' is ignored because it is a " -nonewline
                        Write-Host "WEBRiP" -f darkyellow -nonewline; Write-Host " release."
                    }
                    elseif ($movie_base -match ".*? ") {
                        Write-Host "    Movie '$item' is ignored because it " -nonewline
                        Write-Host "contains spaces" -f darkyellow -nonewline; Write-Host "."
                    }
                    else {
                        Write-Host "    Movie '" -nonewline; Write-Host "$item" -f yellow -nonewline
                        Write-Host "' have no nfo, and none can be copied."
                    }
                }
            }
            else {
                $global:n_w_nfo++
#                Write-Host "    Movie '" -nonewline; Write-Host "$item" -f darkgreen -nonewline
#                Write-Host "' have nfo already."
            }
        }
    }
    $subdirs = Get-ChildItem $WorkPath | where {$_.PSIsContainer}
    ForEach ($subdir in $subdirs) {
        CopyNfos (Join-Path $WorkPath $subdir)
    }
}

CopyNfos $StartPath

function GetNow() {
    return Get-Date -format "yyyy-MM-dd HH:mm:ss"
}

$all_end = Get-Date
$total_time = New-Timespan $all_start $all_end
$now = GetNow
Write-Output "`n"
Write-Output "Finished at $now. Processing took $total_time."
Write-Output "Found $global:n_wo_nfo movies without nfo, copied $global:n_nfo_copied nfo's."
Write-Output "Found $global:n_w_nfo movies with nfo."
